import { ColumnApi, GridApi, GridOptions } from 'ag-grid-community'
import { AdBaseGridContext } from './base.grid.context'
export class AdBaseGridOptions implements GridOptions {
  api: GridApi
  columnApi: ColumnApi
  unSortIcon = true
  headerHeight = 40
  rowHeight = 40
  suppressClickEdit = true
  suppressRowClickSelection = true
  suppressCellSelection = true
  enableCellTextSelection = true
  suppressScrollOnNewData = false
  rowSelection = 'multiple'
  columnDefs = []
  rowData = []
  pinnedBottomRowData = []
  overlayLoadingTemplate = '<!-- loading -->'
  overlayNoRowsTemplate = '<span class="ad-grid-empty-data-placeholder"></span>'
  context: {
    [key: string]: any
    componentParent: any
    superThis: AdBaseGridContext
  } = {
    componentParent: null,
    superThis: null,
  }
  constructor(superThis: AdBaseGridContext) {
    this.context.superThis = superThis
    this.context.componentParent = superThis.componentInstance
  }

  onGridReady = () => {
    /* 展示loading */
    this.api.showLoadingOverlay()
    /* 调用渲染函数 */
    this.context.superThis.onGridReady()
  }

  onRowClicked(event) {
    event.node.setSelected(!event.node.selected)
  }

  /**
   * 自定义行id
   * @param e
   * @returns
   */
  getRowNodeId = (e) => {
    return this.context.superThis.getRowNodeId(e)
  }

  onRowSelected(e) {
    this.context.superThis.onRowSelected(e)
  }

  onSelectionChanged() {
    this.context.superThis.onSelectionChanged()
  }

  /**
   * 数据变更回调
   */
  onRowDataChanged() {
    if (this.api.getRenderedNodes().length == 0) {
      this.api.showNoRowsOverlay()
    }
  }

  onRowDoubleClicked(event) {
    if (event.rowPinned == 'bottom' || event.rowPinned == 'top') {
      return
    }
    this.context.componentParent.rowDoubleClick &&
      this.context.componentParent.rowDoubleClick(event)
  }

  onColumnResized = (e) => {
    if (e.finished === true && e.source === 'uiColumnDragged' && e.type === 'columnResized') {
      e.api.sizeColumnsToFit()
    }
  }

  onGridSizeChanged(e) {
    e.api.sizeColumnsToFit()
  }

  onSortChanged(e) {
    this.context.superThis.onSortChanged(e)
  }
}
